<?php
/*Greg*/

ob_start();
require_once('FirePHPCore/FirePHP.class.php');

function getCategoryFromCropId($cropid){
	if($cropid == null) die("No cropid specified");
	$con = connect_db();
	$sql = "select categoryid from CROP where cropid=$cropid";
	$result = mysql_fetch_array(mysql_query($sql, $con));
	mysql_close($con);
	if(count($result) < 1) die("No crop with cropid=$cropid found");
	
	return $result[0];
}
function getQuantity($cropid){
	if($cropid == null) die("No cropid specified");
	$con = connect_db();
	$sql = "select quantity from AMOUNT_HARVESTED where cropid=$cropid";
	$result = mysql_fetch_array(mysql_query($sql, $con));
	mysql_close($con);
	if(count($result) < 1) die("No crop with cropid=$cropid found");
	return $result[0];
}
function getCost($cropid){
	if($cropid == null) die("No cropid specified");
	$con = connect_db();
	$sql = "select cost from SEEDS_PURCHASED where cropid=$cropid";
	$result = mysql_fetch_array(mysql_query($sql, $con));
	mysql_close($con);
	if(count($result) < 1) die("No crop with cropid=$cropid found");
	return $result[0];
}
function getSeason($cropid){
	if($cropid == null) die("No cropid specified");
	$con = connect_db();
	$sql = "select season from CROP where cropid=$cropid";
	$result = mysql_fetch_array(mysql_query($sql, $con));
	mysql_close($con);
	if(count($result) < 1) die("No crop with cropid=$cropid found");
	return $result[0];
}
function getPlantTime($cropid){
	if($cropid == null) die("No cropid specified");
	$con = connect_db();
	$sql = "select startDate from PLANTS_ADDED where cropid=$cropid";
	$result = mysql_fetch_array(mysql_query($sql, $con));
	mysql_close($con);
	if(count($result) < 1) die("No crop with cropid=$cropid found");
	return $result[0];
}
function getHarvestTime($cropid){
	if($cropid == null) die("No cropid specified");
	$con = connect_db();
	$sql = "select startDate from AMOUNT_HARVESTED where cropid=$cropid";
	$result = mysql_fetch_array(mysql_query($sql, $con));
	mysql_close($con);
	if(count($result) < 1) die("No crop with cropid=$cropid found");
	return $result[0];
}
function getPestIssues($cropid){
	if($cropid == null) die("No cropid specified");
	$con = connect_db();
	$sql = "select notes from PEST_WEED_LOG where cropid=$cropid";
	$result = mysql_fetch_array(mysql_query($sql, $con));
	mysql_close($con);
	if(count($result) < 1) die("No crop with cropid=$cropid found");
	return $result[0];
}
function getAllCropnames() {
	$con = connect_db();
	$sql = "select cropname from CROP";
	$result = mysql_fetch_array(mysql_query($sql, $con));
	mysql_close($con);
	if(count($result) < 1) die("No crop with cropid=$cropid found");
	return $result[0];
}

//11-2-10 Greg 
function getMonth($l) {
	switch ($l) {
		case 1:
			return "January";
			break;
		case 2:
			return "February";
			break;
		case 3:
			return "March";
			break;
		case 4:
			return "April";
			break;
		case 5:
			return "May";
			break;
		case 6:
			return "June";
			break;
		case 7:
			return "July";
			break;
		case 8:
			return "August";
			break;
		case 9:
			return "September";
			break;
		case 10:
			return "October";
			break;
		case 11:
			return "November";
			break;
		case 12:
			return "December";
			break;
		default:
			throw new Exception("Error finding month");
	}
}

function getMonthNumber($month) {
	switch ($month) {
		case "January":
			return 01;
			break;
		case "February":
			return 02;
			break;
		case "March":
			return 03;
			break;
		case "April":
			return 04;
			break;
		case "May":
			return 05;
			break;
		case "June":
			return 06;
			break;
		case "July":
			return 07;
			break;
		case "August":
			return 8;
			break;
		case "September":
			return 9;
			break;
		case "October":
			return 10;
			break;
		case "November":
			return 11;
			break;
		case "December":
			return 12;
			break;
		default:
			print $month;
			throw new Exception("Error finding month");
	}
}

function lastDayOfMonth($l) {
	switch ($l) {
	case 1:
		return "31";
		break;
	case 2:
		return "28"; //FIX!!!!!!!!!!!!!!!!!!!! ---> leap year issue
		break;
	case 3:
		return "31";
		break;
	case 4:
		return "30";
		break;
	case 5:
		return "31";
		break;
	case 6:
		return "30";
		break;
	case 7:
		return "31";
		break;
	case 8:
		return "31";
		break;
	case 9:
		return "30";
		break;
	case 10:
		return "31";
		break;
	case 11:
		return "30";
		break;
	case 12:
		return "31";
		break;
	default:
		throw new Exception("Error finding month");
	}
}

function getAllAttributes() {
	return array ("amount_harvested" => "Amount Harvested", "amount_donated" => "Amount Donated", "sales" => "Sales", "number_planted" => "Number Planted", "number_transplanted" => "Number Transplanted", "seeds_bought" => "Seeds Bought", "pest_issues" => "Pest Issues", "weed_issues" => "Weed Issues" );
}

//makes it pretty
function attributeFormat($attr) {
	if ($attr == "amount_donated") {
		return "Amount Donated";
	}
	else if ($attr == "sales") {
		return "Sales";
	}
	else if ($attr == "amount_harvested") {
		return "Amount Harvested";
	}
	else {
		return $attr;
	}
}

function getMultiYearCropData($startTime, $endTime, $cropId, $attr, $unit = NULL, $customer = NULL) {
	if ($attr == 'seeds_bought') {
		return getGeneralMultiYearCropData($startTime, $endTime, $cropId, 's');
	}
	if ($attr == 'number_planted') {
		return getGeneralMultiYearCropData($startTime, $endTime, $cropId, 'p');
	}
	if ($attr == 'number_transplanted') {
		return getGeneralMultiYearCropData($startTime, $endTime, $cropId, 't');
	}
	if ($attr == 'amount_harvested') {
		return getHarvestMultiYearCropData($startTime, $endTime, $cropId, $unit);
	}
	if ($attr == 'sales') {
		return getSalesMultiYearCropData($startTime, $endTime, $cropId, $unit, $customer);
	}
	if ($attr == 'amount_donated') {
		return getDonationMultiYearCropData($startTime, $endTime, $cropId, $unit, $customer);
	}
}

function getRangeData($startTime, $endTime, $cropId, $attr, $unit = NULL, $customer = NULL) {
	if ($attr == 'seeds_bought') {
		return getGeneralRangeData($startTime, $endTime, $cropId, 's');
	}
	if ($attr == 'number_planted') {
		return getGeneralRangeData($startTime, $endTime, $cropId, 'p');
	}
	if ($attr == 'number_transplanted') {
		return getGeneralRangeData($startTime, $endTime, $cropId, 't');
	}
	if ($attr == 'amount_harvested') {
		return getHarvestRangeData($startTime, $endTime, $cropId, $unit);
	}
	if ($attr == 'sales') {
		return getSalesRangeData($startTime, $endTime, $cropId, $unit, $customer);
	}
	if ($attr == 'amount_donated') {
		return getDonationRangeData($startTime, $endTime, $cropId, $unit, $customer);
	}
}
function getRangeWeeklyCropData($startTime, $endTime, $cropId, $attr, $unit = NULL, $customer = NULL) {
	if ($attr == 'seeds_bought') {
		return getGeneralRangeWeeklyCropData($startTime, $endTime, $cropId, 's');
	}
	if ($attr == 'number_planted') {
		return getGeneralRangeWeeklyCropData($startTime, $endTime, $cropId, 'p');
	}
	if ($attr == 'number_transplanted') {
		return getGeneralRangeWeeklyCropData($startTime, $endTime, $cropId, 't');
	}
	if ($attr == 'amount_harvested') {
		return getHarvestRangeWeeklyCropData($startTime, $endTime, $cropId, $unit);
	}
	if ($attr == 'sales') {
		return getSalesRangeWeeklyCropData($startTime, $endTime, $cropId, $unit, $customer);
	}
	if ($attr == 'amount_donated') {
		return getDonationRangeWeeklyCropData($startTime, $endTime, $cropId, $unit, $customer);
	}
}